<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./js/vue.js"></script>
</head>

<body>
    <div id="app">
        <select v-model="provinceId">
            <option value="">请选择省份</option>
            <option :value="item.id" v-for="item  in provinceList">{{item.name}}</option>
        </select>
        <select v-model="cityId">
            <option value="">请选择城市</option>
            <option :value="item.id" v-for="item in cityList">
                {{item.name}}
            </option>
        </select>
        <select v-model="townId">
            <option value="">请选择区县</option>
            <option :value="item.id" v-for="item in townList">
                {{item.name}}
            </option>
        </select>
    </div>
</body>
<script>
    new Vue({
        el: "#app",
        data: {
            provinceId: "",//省份的编号
            cityId: "",//城市的编号
            townId:"",//区县的编号

            list: [{ id: 1, name: "湖北", pid: 0 },
            { id: 2, name: "河南", pid: 0 },
            { id: 3, name: "武汉", pid: 1 },
            { id: 4, name: "荆州", pid: 1 },
            { id: 5, name: "江夏", pid: 3 },
            { id: 6, name: "洪山", pid: 3 },
            { id: 7, name: "沙市", pid: 4 },
            { id: 8, name: "松滋", pid: 4 },
            { id: 9, name: "郑州", pid: 2 },
            { id: 10, name: "洛阳", pid: 2 },
            { id: 11, name: "洛阳的区一", pid: 10 },
            { id: 12, name: "洛阳的区二", pid: 10 },
            { id: 13, name: "新郑", pid: 9 },
            { id: 14, name: "金水", pid: 9 }
            ]
        },
        watch:{
            provinceId(){
                this.townId = "";
                this.cityId= "";
            },
            cityId(){
                this.townId = "";
            }


        },
        computed: {
            provinceList() {
                return this.list.filter(item => item.pid == 0);
            },
            cityList() {
                return this.provinceId ? this.list.filter(item => item.pid == this.provinceId) : "";
            },
            townList(){
                return  this.cityId ? this.list.filter(item=>item.pid == this.cityId) :"";
            }

        }


    })

</script>

</html>